<?php namespace App\Http\Controllers\admin\config;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use Input;
use App\Models\Techs;
use App\Models\InvestmentType;
use App\Models\MainBody;
use App\Models\UserRoles;
use App\Models\System;
use App\Models\Domains;
use Redirect;
use Session;



class ConfigController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */

	public function index()
	{
		if(Session::has('admin')){
		return view("admin.config.index")->withIsFrontbase(true);
	    }else{
	    	return Redirect::to('admin/dologin');
	    }
	}

	public function addTech()
	{
		$str = Input::get('data');
		$str = preg_replace("/[\r\n|]/", ',', $str);
		$arr = explode(',', $str);
		foreach ($arr as $key => $value) {
			if($value){
				Techs::create(["cate_name" => $value]);
			}
		}
		return $this->json([]);
	}

	public function addType(){
		$str = Input::get("data");
		$str = preg_replace("/[\r\n|]/", ',', $str);
		$arr = explode(',',  $str);
		foreach ($arr as $key => $value) {
			if($value){
				InvestmentType::create(["name" => $value]);
			}
		}
		return $this->json([]);
	}
	public function addInvest(){
		$str = Input::get("data");
		$str = preg_replace("/[\r\n|]/", ',', $str);
		$arr = explode(',',  $str);
		foreach ($arr as $key => $value) {
			if($value){
				InvestmentType::create(["name" => $value]);
			}
		}
		return $this->json([]);

	}

	public function addDomains(){
		$str = Input::get("data");
		$parent_id = Input::get("parent_id", 0);
		$str = preg_replace("/[\r\n|]/", ',', $str);
		$arr = explode(',', $str);
		foreach ($arr as $key => $value) {
			if($value){
				Domains::create(["cate_name"=>$value, "parent_id" => $parent_id]);
            }
		}
		return $this->json([]);
	}


	public function addBody(){
		$str = Input::get("data");
		$str = preg_replace("/[\r\n|]/", ',', $str);
		$arr = explode(',', $str);
		foreach ($arr as $key => $value) {
			if($value){
				MainBody::create(["name" => $value]);
			}
		}
		return $this->json([]);
	}


	public function getInvest(){
		$data = InvestmentType::all()->toArray();
		return $this->json(["data"=>$data]);
	}


	public function deleteInvest(){
		$arr = Input::get("inves");
		$model= InvestmentType::whereIn("id",$arr);
		if($model->count()){
			$model->delete();
			return $this->json([]);
		}else{
			return;
		}
	}


	public function getTech(){

		$data = Techs::all()->toArray();
		return $this->json(["data"=>$data]);

	}

	public function getDomians(){
		$parent_id = Input::get("parent_id", 0);
		$model = Domains::where("parent_id", $parent_id);
		$data = $model->get()->toArray();
		return $this->json(["data" => $data]);
	}


	public function getBody(){
		$data = MainBody::all()->toArray();
		return $this->json(["data"=>$data]);
	}

	public function getRole(){

		$data = UserRoles::all()->toArray();
		return $this->json(["data"=>$data]);
	}

	public function getSystem(){

		$data = System::all()->toArray();
		return $this->json(["data"=>$data]);
	}


	public function deleteSystem()
	{
         $arr = Input::get("systems");
         $model = System::whereIn("id", $arr);
          if($model->count()){
          	$model->delete();
         	return $this->json([]);
          }else{
          	return;
          }
	}




	public function deleteTech(){

         $arr = Input::get("techs");

         $model = Techs::whereIn("id", $arr);
         if($model->count()){
         	$model->delete();
        	return $this->json([]);
         }else{
         	return;
         }
	}


    public function deleteDomians(){

    	$arr = Input::get("cid");
    	$model = Domains::where("id",$arr);
    	if($model->count()){
    		$model->delete();
    		return $this->json([]);
    	}else{
    		return;
    	}
    }


	public function deleteBody(){

		$id = Input::get("applys");
		$model = MainBody::whereIn("id",$id);
		if($model->count()){
			$model->delete();
			return $this->json([]);
		}else{
			return;
		}

	}







	public function updateTech()
	{

	  	$id = Input::get('tid');
	  	$name = Input::get("name");
	  	$model = Techs::where("id",$id);
	  	if($model->count()){
	  		$model->update(["cate_name"=>$name]);
	  		return $this->json(["data" => $name]);
	  	}else{
	  		return;
	  	}

	}

	public function updateDomains(){

		$id = Input::get('tid');
		$name = Input::get("name");
		$model = Domains::where("id",$id);
		if($model->count()){
			$model->update(["cate_name"=>$name]);
			return $this->json(["data" => $name]);
		}else{
			return;
		}
	}

	public function updateRole(){
		$id = Input::get("tid");
		$name = Input::get("role_name");

		$model = UserRoles::where("id",$id);
		if($model->count()){
			$model->update(["role_name"=>$name]);
			return $this->json(["data" => $name]);
		}else{
			return;
		}
	}

	public function addSystem()
	{
		$data = Input::all();
		System::create($data);
		return $this->json([]);

	}

	public function updateSystem(){

		$id = Input::get("tid");
		$data = Input::except("tid");
		$model = System::where("id",$id);
		if($model->count()){
			$model->update($data);
			return $this->json([]);
		}else{
			return;
		}
	}

	public function updateInvest(){

		$id = Input::get("tid");
		$name = Input::get("name");
		$model = InvestmentType::where("id",$id);
		if($model->count()){
			$model->update(["name"=>$name]);
			return $this->json(["data" => $name]);
		}else{
			return;
		}
	}
	public function updateBody(){

		$id = Input::get("tid");
		$name = Input::get("name");
		$model = MainBody::where("id",$id);
		if($model->count()){
			$model->update(["name"=>$name]);
			return $this->json(["data" => $name]);
		}else{
			return;
		}
	}




	public function getDomains()
	{
		$data = Domains::get()->toArray();
		$temp = array();
		$cid = 0;
		$temp = $this->get_son($data,$cid);
		return $this->json(['data'=>$temp]);
	}


	public function get_son($data,$cid)
	{
		$temp = array();
		foreach ($data as $v) {
			if($v['parent_id'] == $cid){
				$temp[] = $v;
				$temp = array_merge($temp,$this->get_son($data,$v['id']));
			}
		}
		return $temp;
	}





	   // public function updateRoles(){
	   // 	   $id = Input::get("");
	   // 	   $name = Input::get("");
	   // 	   $model = UserRoles::where("id",$id);
	   // 	   if($model->count()){
	   // 	   	$model->update(["role_name"=>$name]);
	   // 	   }
	   // }


	// public function  getRoles(){

	// 	$data = UserRoles::get("data")->toArray();
	// 	return $this->json(["date"=>$data]);
	// }

	// public function updateSystem(){

	// 	$id = Input::get("data");
	// 	$key = Input::get("");
	// 	$value = Input::get("");
	// 	$model = System::where("id",$id);
	// 	if($model->count()){
	// 		$model->update(["key"=>$key,"value"=>$value]);
	// 	}
	// }

	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}



}
